package com.gaoding.qa.auto.config;

import com.dtflys.forest.Forest;
import com.dtflys.forest.config.ForestConfiguration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.gaoding.commons.util.JsonMapper;
import com.gaoding.commons.util.SpringUtil;
import com.gaoding.qa.auto.condition.SuccessCondition;
import com.gaoding.qa.auto.converter.JsonConverter;
import com.gaoding.qa.auto.help.TestHelper;
import com.gaoding.qa.auto.interceptor.CookieInterceptor;
import com.gaoding.qa.auto.interceptor.CoreInterceptor;
import com.gaoding.qa.auto.properties.GlobalProperties;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @author mobai
 * @since 2021/9/22 15:09
 */
@Configuration
@Slf4j
public class CoreConfig {
    @PostConstruct
    public void init(){
        ForestConfiguration config = Forest.config();
        ObjectMapper mapper = JsonMapper.defaultMapper().getMapper();
        // 日期和时间格式化
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        mapper.registerModule(javaTimeModule);
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        config.setJsonConverter(new JsonConverter());
        config.setInterceptors(Lists.newArrayList(CoreInterceptor.class, CookieInterceptor.class));
        config.setSuccessWhenClass(SuccessCondition.class);
        //如果配置了需要自动生成token，则启动时则会自动生成token
        GlobalProperties globalProperties = SpringUtil.getBean(GlobalProperties.class);
        Boolean isNeedAutoToken = globalProperties.getIsNeedAutoToken();
        if (isNeedAutoToken!=null && isNeedAutoToken) {
            log.info("启动自动生成token,账号:{},密码：{}",globalProperties.getAccount(),globalProperties.getPassword());
            try {
                TestHelper.generateTokenWithMultiOrg(globalProperties.getAccount(), globalProperties.getPassword()
                        , true, globalProperties.getEnvironment());
            }catch (Exception e){
                log.error("自动生成token失败,账号:{},密码:{},原因:{}",globalProperties.getAccount(),globalProperties.getPassword(),e.getMessage(),e);
            }
            RuntimeContext context = TestHelper.getContext();
            Map<String, String> dynamicHeaders = context.getDynamicHeaders();
            globalProperties.getHeaders().putAll(dynamicHeaders);
        }

    }
}